package com.redsoft.epip.handler;

import java.util.Date;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.google.gson.Gson;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * 
 * 微信用户发送消息事件推送
 * 
 * <pre>
 * 微信用户发送消息事件推送
 * </pre>
 * 
 * @author 杨雷
 * @date 2017年10月10日
 * @since 1.0
 *
 */
@Component
public class MsgHandler extends AbstractHandler {

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
			Map<String, Object> context, WxMpService weixinService,
			WxSessionManager sessionManager) throws WxErrorException {
		System.out.println("=============>微信用户发送消息事件推送: " + wxMessage.getEvent());
		
		if (WxConsts.CUSTOM_MSG_TEXT.equals(wxMessage.getMsgType())) {
			return handleUserMsg(wxMessage, weixinService);
		}
		return null;
	}
	
	public WxMpXmlOutMessage handleUserMsg(WxMpXmlMessage wxMessage, WxMpService weixinService) {
		WxMpXmlOutMessage outMessage = WxMpXmlOutMessage.TEXT().toUser(wxMessage.getFromUser()).fromUser(wxMessage.getToUser()).build();
		outMessage.setCreateTime(new Date().getTime());
		outMessage.setMsgType("transfer_customer_service");
		
		System.out.println("--------" + new Gson().toJson(outMessage));
		return outMessage;
	}

}
